var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var syslog = require('./common/sylog.js');
var loggersFile = syslog.loggerf('CDZS');
var loggers = syslog.loggerf('');
var routers = require('./routes/index');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server)
global.loggersFile = loggersFile;
global.loggers = loggers;

require('./common/commonModel');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


// app.use(function(req, res, next){
//  res.io = io;
//  next();
// });



/**
 * client {
 *    roomId
 *    id
 *    score
 * }
 */
var question = require("./controller/question")
var ioInfo = require("./controller/io")

question.init(io)
ioInfo.init(io)

// io.on('connection', function (client) {
//   /// 进入房间，记录用户信息
//   client.on('inRoom', function(data){
//     console.log("333333333333333",data);
//
//     client.accountid=data.accountid;
//
//     let roomType = data.roomType;
//     let roomInfo = global.roomsAccount[roomType]
//
//     if (!roomInfo.clients[roomInfo.roomNum])  roomInfo.clients[roomInfo.roomNum] =[];
//
//     if (roomInfo.clients[roomInfo.roomNum].length >= roomType){
//         roomInfo.roomNum += 1;
//         roomInfo.clients[roomInfo.roomNum] =[];
//     }
//       roomInfo.clients[roomInfo.roomNum].push(client)
//
//     client.roomId = roomInfo.roomNum;
//     client.roomType = roomType;
//     client.score = 0;
//     console.log("roomInfo.roomNum]",roomInfo.roomNum);
//     let ClientsInfo  = roomInfo.clients[roomInfo.roomNum]
//     console.log("numChange",ClientsInfo.length);
//     for (var clinetInfo  of ClientsInfo ){
//       /// 通知客户端人数变化
//       console.log("通知客户端人数变化")
//       //io.emit("numChange",ClientsInfo.length)
//      // client.emit('login',{onlineUser:1,onlineCount:1,user:{}});
//       clinetInfo.emit("numChange",{onlineUser:1,onlineCount:1,user:{userid:1,username:333}})
//     }
//     /// 通知客户端开始答题
//     if(ClientsInfo.length == roomType){
//       client.emit("startQuestion",clinetInfo.length)
//       question.startQuestion(roomInfo,roomInfo.roomNum)
//     }
//   });
//
//   /// 开始答题
//   client.on('answer', function(data){
//     console.log("answeransweransweranswer",data);
//     console.log("answeransweransweranswer",client.roomId);
//     console.log("answeransweransweranswer",client.roomType);
//     let questions = global.roomsAccount[client.roomType]['questions'][client.roomId]
//
//     let questionId = data.questionId;
//     let answer = data.answer;
//     console.log("questionId",questionId);
//     console.log("answer",answer);
//     console.log("questions",questions);
//
//     if(questionId == questions.id &&  answer == questions.answer){
//       /// 分数+1
//       client.score += 1;
//       console.log("client.score",client.score);
//       console.log("client.roomId",client.roomId);
//       console.log("client.roomType",client.roomType);
//     }
//   });
//
//   client.on('disconnect', function(){
//     console.log("disconnect client.accountid",client.accountid);
//     console.log("disconnect client.score",client.score);
//     console.log("disconnect client.roomId",client.roomId);
//     console.log("disconnect client.roomType",client.roomType);
//     // let clients = global.roomsAccount[client.roomType][client.roomId]
//     // for(var i in clients){
//     //   if(clients[i].accountid == client.accountid){
//     //     clients.splice(i,1);
//     //   }
//     // }
//   });
//
//   client.on('error', function(){
//     console.log("error client.score",client.score);
//     console.log("error client.roomId",client.roomId);
//     console.log("error client.roomType",client.roomType);
//
//   });
// });
//
routers.init({
  app:app,
});


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
var port = require("./common/config.json").port;
app.set('port', port);
loggers.info(" Server Start On port:",port)
server.listen(port);
module.exports = app;
